#include "vcd-topbar.h"

#include <gtk/gtk.h>
#include <glib.h>

#ifdef GDK_WINDOWING_X11
#include <X11/Xlib.h>
#include <gdk/gdkx.h>
#include <X11/extensions/shape.h>
#endif

#ifdef GDK_WINDOWING_WIN32
#include <gdk/gdkwin32.h>
#endif

#include "vcd-linux-ui/vcd-window-timed-revealer.h"
#include "vcd-common-api/vcd-log.h"

#define VCD_TOPBAR_TOP_OUT_Y -56

static void _set_css(GtkWidget *widget, const gchar *data_css)
{
    GtkCssProvider *provider = gtk_css_provider_new ();
    GtkStyleContext *context = gtk_widget_get_style_context(widget);
    gtk_css_provider_load_from_data(provider, data_css, -1, NULL);
    gtk_style_context_add_provider(context, GTK_STYLE_PROVIDER(provider), GTK_STYLE_PROVIDER_PRIORITY_USER);
    g_object_unref(provider);
}

static GdkPixbuf* vcd_topbar_new_GdkPixbuf(const char* path,int width,int height,GdkPixbufRotation rotation){

    GdkPixbuf* gpb;

    gpb = gdk_pixbuf_new_from_file(path,0);
    if(!gpb) return 0;

    gpb = gdk_pixbuf_scale_simple(gpb,width,height,GDK_INTERP_HYPER);
    gpb = gdk_pixbuf_rotate_simple(gpb,rotation);

    return gpb;
}

static GdkPixbuf* vcd_topbar_new_GdkPixbuf_rotation(const char* path,int width,int height,GdkPixbufRotation rotation){

    GdkPixbuf* gpb;

    gpb = gdk_pixbuf_new_from_file(path,0);
    if(!gpb) return 0;

    gpb = gdk_pixbuf_scale_simple(gpb,width,height,GDK_INTERP_HYPER);
    gpb = gdk_pixbuf_rotate_simple(gpb,rotation);

    return gpb;
}

static GtkWidget* vcd_topbar_new_gtkimage(const char* path,int width,int height,TopbarPostion position){

    GdkPixbuf* gpb;
    GtkWidget* image;

    gpb = gdk_pixbuf_new_from_file(path,0);
    if(!gpb) return 0;

    gpb = gdk_pixbuf_scale_simple(gpb,width,height,GDK_INTERP_HYPER);

    if(position == POSITION_LEFT)
        gpb = gdk_pixbuf_rotate_simple(gpb,GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE);
    else if(position == POSITION_RIGHT)
        gpb = gdk_pixbuf_rotate_simple(gpb,GDK_PIXBUF_ROTATE_CLOCKWISE);
    else
        gpb = gdk_pixbuf_rotate_simple(gpb,GDK_PIXBUF_ROTATE_NONE);


    image = gtk_image_new_from_pixbuf(gpb);
    return image;
}

void vcd_topbar_get_no_dpi_size(int w,int h,int* nw,int* nh){

    // 高分屏缩放
    // 100% — 每英寸 96  像素
    // 125% — 每英寸 120 像素
    // 150% — 每英寸 144 像素
    // 200% — 每英寸 192 像素
    #define DEFAULT_DPI 96

    GdkScreen *screen = gdk_screen_get_default();
    double rl = gdk_screen_get_resolution(screen);
    double s = rl / DEFAULT_DPI;

    int w1 = w / s;
    int h1 = h / s;

    *nw = w1;
    *nh = h1;
}

void vcd_topbar_get_screen_base_size(int* width,int* height){
    // 获取主屏幕的宽高
    GdkScreen *screen = gdk_screen_get_default();
    GdkMonitor *monitor = gdk_display_get_primary_monitor(gdk_display_get_default());

    // 获取主屏幕宽高
    GdkRectangle geometry;
    gdk_monitor_get_geometry(monitor,&geometry);

    // 高分屏缩放
    // 100% — 每英寸 96  像素
    // 125% — 每英寸 120 像素
    // 150% — 每英寸 144 像素
    // 200% — 每英寸 192 像素
    #define DEFAULT_DPI 96

    double rl = gdk_screen_get_resolution(screen);
    double s = rl / DEFAULT_DPI;

    int w = geometry.width / s;
    int h = geometry.height / s;

    *width = w;
    *height = h;
}

void vcd_topbar_adapter_spice_size(int w,int h,int* nw,int* nh){
    // max size of spice support
    int maxW = 2560;
    int maxH = 1600;

    if(w <= maxW && h <= maxH){
        *nw = w;
        *nh = h;
        return;
    }

    float sw = w * 1.0 / maxW;
    float sh = h * 1.0 / maxH;
    float ss = sw > sh ? sw : sh;

    int tw = w / ss;
    int th = h / ss;

    if(tw % 2 != 0)
        tw++;
    if(th % 2 != 0)
        th++;

    *nw = tw;
    *nh = th;
}

typedef struct vcd_topbar{
    VcdTopbarCtx* context;
    GtkWidget* toplevel;
    GtkWidget*  revealer;
    GtkWidget* image;
    int width;
    int height;
}VcdTopbar;


static guint g_timeout_in;
static guint g_timeout_out;

static gboolean vclient_toplevel_timer_in_func(gpointer* arg)
{
    VcdTopbar* topbar = (VcdTopbar*)arg;
    TopbarPostion position = topbar->context->position;

    GtkWidget* widget = topbar->toplevel;
    int gx,gy,gw,gh;

    gdk_window_get_geometry(gtk_widget_get_window(widget),&gx,&gy,&gw,&gh);

    fprintf(stderr,"[timer in] [%d %d %d %d]\n",gx,gy,gw,gh);

    if(position == POSITION_TOP){

        if(gy >= 0){
            g_timeout_in = 0;
            return FALSE;
        }

        gtk_window_move(GTK_WINDOW(widget),gx,gy + 2);
    }else if(position == POSITION_LEFT){
        if(gx >= 0){
            g_timeout_in = 0;
            return FALSE;
        }
        gtk_window_move(GTK_WINDOW(widget),gx + 1,gy);
    }else{
        int sw = gdk_screen_get_width(gdk_screen_get_default());
        if(gx <= sw-topbar->width){
            g_timeout_in = 0;
            return FALSE;
        }
        gtk_window_move(GTK_WINDOW(widget),gx - 1,gy);
    }
    return TRUE;
}

static gboolean vclient_toplevel_timer_out_func(gpointer* arg)
{
    VcdTopbar* topbar = (VcdTopbar*)arg;
    TopbarPostion position = topbar->context->position;
    GtkWidget* widget = topbar->toplevel;
    int gx,gy,gw,gh;

    gdk_window_get_geometry(gtk_widget_get_window(widget),&gx,&gy,&gw,&gh);
    //fprintf(stderr,"[timer out][%d %d %d %d]\n",gx,gy,gw,gh);

    int x,y;
    gdk_window_get_position(gtk_widget_get_window(widget),&x,&y);

    if(position == POSITION_TOP){
        if(gy <= VCD_TOPBAR_TOP_OUT_Y){
            g_timeout_out = 0;
            return FALSE;
        }

        gtk_window_move(GTK_WINDOW(widget),gx,gy - 2);

    }else if(position == POSITION_LEFT){
        if(gx <= -1*topbar->width + 2){
            g_timeout_out = 0;
            return FALSE;
        }
        gtk_window_move(GTK_WINDOW(widget),gx - 2,gy);
    }else{
        int sw = gdk_screen_get_width(gdk_screen_get_default());
        if(gx >= sw - 2){
            g_timeout_out = 0;
            return FALSE;
        }
        gtk_window_move(GTK_WINDOW(widget),gx + 1,gy);
    }
    return TRUE;
}

static void _on_clicked_usb(GtkToolButton *toolbutton G_GNUC_UNUSED,gpointer user_data){
    VcdTopbar* vcdtopbar = (VcdTopbar*)user_data;
    VcdTopbarCtx* context = vcdtopbar->context;

    if(context && context->on_usb_redir){
        context->on_usb_redir(0,context->priv);
    }
}

static void _on_clicked_fullscreen(GtkToolButton *toolbutton ,gpointer user_data){
    static gboolean b;

    b = !b;

    const char* icon = b ? "topbar-fullscreen.png" : "topbar-no-fullscreen.png";
    gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(toolbutton),vcd_revealer_new_gtkimage(vcd_revealer_get_img_full_path(icon),24,24));
    gtk_widget_show_all(GTK_WIDGET(toolbutton));

    VcdTopbar* vcdtopbar = (VcdTopbar*)user_data;
    VcdTopbarCtx* context = vcdtopbar->context;
    if(context && context->on_fullscreen){
        context->on_fullscreen(0,context->priv);
    }
}

static void _on_clicked_reconnect(GtkToolButton *toolbutton G_GNUC_UNUSED,gpointer user_data){

    VcdTopbar* vcdtopbar = (VcdTopbar*)user_data;
    VcdTopbarCtx* context = vcdtopbar->context;

    #ifdef __linux__
    char cmd[100] = {0};
    sprintf(cmd,"kill -%d  $(ps -ef | grep verystream | grep -v grep |  awk '{printf $2}')",SIGUSR1);
    system(cmd);
    #endif

    if(context && context->on_reconnect){
        context->on_reconnect(0,context->priv);
    }
}

static void _on_clicked_close(GtkToolButton *toolbutton G_GNUC_UNUSED,gpointer user_data){

    VcdTopbar* vcdtopbar = (VcdTopbar*)user_data;
    VcdTopbarCtx* context = vcdtopbar->context;

    #ifdef __linux__
    char *cmd = "killall verystream";
    system(cmd);
    #endif

    if(context && context->on_close){
        context->on_close(0,context->priv);
    }
}

static GtkWidget* vcd_topbar_item_make(GtkWidget* box,const char* label_text,const char* image_name,GCallback call_back,gpointer priv){

    GtkWidget* toolbar = NULL;
    GtkWidget* button = NULL;

    toolbar = gtk_toolbar_new();
    gtk_widget_set_size_request(toolbar,20,20);

    gtk_toolbar_set_style(GTK_TOOLBAR(toolbar),GTK_TOOLBAR_BOTH);
    button = GTK_WIDGET(gtk_tool_button_new(NULL, NULL));
    gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(button),vcd_revealer_new_gtkimage(vcd_revealer_get_img_full_path(image_name),24,24));
    gtk_tool_button_set_label_widget(GTK_TOOL_BUTTON(button),gtk_label_new(label_text));
    gtk_toolbar_insert(GTK_TOOLBAR(toolbar), GTK_TOOL_ITEM(button), 0);
    gtk_box_pack_start(GTK_BOX(box),toolbar,TRUE,TRUE,0);

    if(call_back)
         g_signal_connect(button, "clicked", G_CALLBACK(call_back), priv);

    GtkWidget* label = gtk_tool_button_get_label_widget(GTK_TOOL_BUTTON(button));
    _set_css(label, "label{font-size:small;color:#000000}");
    _set_css(toolbar,"toolbar{border: none;padding:0px}");

     return toolbar;
}

static GtkWidget* vcd_topbar_buttons_new(VcdTopbar* vcdtopbar){
    GtkOrientation orientation = vcdtopbar->context->position==POSITION_TOP ? GTK_ORIENTATION_HORIZONTAL:GTK_ORIENTATION_VERTICAL;
    GtkWidget* box = gtk_box_new(orientation,0);

    GtkWidget* usb =  vcd_topbar_item_make(box,"usb设备","topbar-usb.png",G_CALLBACK(_on_clicked_usb),vcdtopbar);
    //vcd_topbar_item_make(box,"强制关机","topbar-shutdown.png",vcdtopbar->context->disable_btn_shutdown,vcdtopbar);
    vcd_topbar_item_make(box,"刷新重连","topbar-reconnect.png",G_CALLBACK(_on_clicked_reconnect),vcdtopbar);
    #ifndef __linux__
    vcd_topbar_item_make(box,"退出全屏","topbar-no-fullscreen.png",G_CALLBACK(_on_clicked_fullscreen),vcdtopbar);
    #endif
    vcd_topbar_item_make(box,"断开连接","topbar-close.png",G_CALLBACK(_on_clicked_close),vcdtopbar);

    if(vcdtopbar->context->disable_btn_usbredir){
        gtk_widget_set_sensitive(usb,FALSE);
    }

    return box;
}

static void vcd_topbar_init(VcdTopbar* topbar,VcdTopbarCtx* context){
    #define FLOATBAR_DEFAULT_WIDTH 200
    #define FLOATBAR_DEFAULT_HEIGHT 30

    topbar->context = context;

    topbar->width = FLOATBAR_DEFAULT_WIDTH;
    topbar->height = FLOATBAR_DEFAULT_HEIGHT;

    if(topbar->context->position != POSITION_TOP ){
        topbar->width = FLOATBAR_DEFAULT_HEIGHT;
        topbar->height = FLOATBAR_DEFAULT_WIDTH;
    }
}

static gboolean _button_pressed(GtkWidget *widget G_GNUC_UNUSED,GdkEventCrossing *event G_GNUC_UNUSED,gpointer user_data){

    return FALSE;
}

static gboolean _button_release(GtkWidget *widget G_GNUC_UNUSED,GdkEventCrossing *event G_GNUC_UNUSED,gpointer user_data G_GNUC_UNUSED){

    VcdTopbar* topbar = user_data;
    GtkWidget* toplevel = topbar->toplevel;
    GtkWidget* revealer = topbar->revealer;
    GtkWidget* image = topbar->image;
    TopbarPostion position = topbar->context->position;

    static GdkPixbuf* gpb0;
    static GdkPixbuf* gpb1;
    static gboolean   b;

    if(position == POSITION_LEFT){
        if(!gpb0)
            gpb0 = vcd_topbar_new_GdkPixbuf(vcd_revealer_get_img_full_path("topbar-arrow-up-black.png"),24,12,GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE);
        if(!gpb1)
            gpb1 = vcd_topbar_new_GdkPixbuf(vcd_revealer_get_img_full_path("topbar-arrow-down-black.png"),24,12,GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE);
    }else if(position == POSITION_RIGHT){
        if(!gpb0)
            gpb0 = vcd_topbar_new_GdkPixbuf(vcd_revealer_get_img_full_path("topbar-arrow-up-black.png"),24,12,GDK_PIXBUF_ROTATE_CLOCKWISE);
        if(!gpb1)
            gpb1 = vcd_topbar_new_GdkPixbuf(vcd_revealer_get_img_full_path("topbar-arrow-down-black.png"),24,12,GDK_PIXBUF_ROTATE_CLOCKWISE);
    }else {
        if(!gpb0)
            gpb0 = vcd_topbar_new_GdkPixbuf(vcd_revealer_get_img_full_path("topbar-arrow-up-black.png"),24,12,GDK_PIXBUF_ROTATE_NONE);
        if(!gpb1)
            gpb1 = vcd_topbar_new_GdkPixbuf(vcd_revealer_get_img_full_path("topbar-arrow-down-black.png"),24,12,GDK_PIXBUF_ROTATE_NONE);
    }

    b = !b;
    GdkPixbuf* gpb = b ? gpb0 : gpb1;
    gtk_image_set_from_pixbuf(GTK_IMAGE(image),gpb);


    if(1 || !toplevel){
        gboolean b = gtk_revealer_get_reveal_child(GTK_REVEALER(revealer));
        gtk_revealer_set_reveal_child(GTK_REVEALER(revealer),!b);
        return FALSE;
    }

//    if(!g_timeout_in && !g_timeout_out){
//        if(b){
//            if(!g_timeout_in)
//                g_timeout_in = g_timeout_add(10,(GSourceFunc)vclient_toplevel_timer_in_func,user_data);
//        }else{
//            if(!g_timeout_out)
//                g_timeout_out = g_timeout_add(10,(GSourceFunc)vclient_toplevel_timer_out_func,user_data);
//        }
//    }


    return FALSE;
}

static gboolean _image_enter_notify(GtkWidget *widget G_GNUC_UNUSED,GdkEventCrossing *event G_GNUC_UNUSED,gpointer user_data){

    GtkWidget* image = user_data;
    _set_css(image,"image{background-color:rgb(230,230,230);border-radius:0px;border:none;margin:0px}");
    return FALSE;

}
static gboolean _image_leave_notify(GtkWidget *widget G_GNUC_UNUSED,GdkEventCrossing *event G_GNUC_UNUSED,gpointer user_data){
    GtkWidget* image = user_data;
    _set_css(image,"image{background-color:rgb(240,240,240);border-radius:0px;border:none;}");
    return FALSE;
}

static void vcd_topbar_set_parent(VcdTopbar* topbar){
    // reparent
    if(!topbar || !topbar->context || !topbar->context->parent_winid)
        return;

#ifdef GDK_WINDOWING_X11
    Window parentId = topbar->context->parent_winid;
    Window child = gdk_x11_window_get_xid(gtk_widget_get_window(topbar->toplevel));
    Display* display = XOpenDisplay(NULL);

    // get parent x,y,w,h
    XWindowAttributes attr;
    XGetWindowAttributes(display, parentId, &attr);

    XWindowAttributes attr_topbar;
    XGetWindowAttributes(display, child, &attr_topbar);

    fprintf(stderr,"attr: [ %d %d %d %d ] \n",attr_topbar.x,attr_topbar.y,attr_topbar.width,attr_topbar.height);

    int x = attr.x + attr.width / 2 - topbar->width / 2;
    //int y = -1 * topbar->height + 2;
    int y = 0;

    if(topbar->context->position == POSITION_LEFT){
        x = 0;
        y = attr.height / 2 - topbar->height / 2;
    }else if(topbar->context->position == POSITION_RIGHT){
        x = attr.width;
        y = attr.height / 2 - topbar->height / 2;
    }

    XReparentWindow(display,child,parentId,x,y);
    XRaiseWindow(display,child);
    XFlush(display);

    fprintf(stderr,"vcd_topbar_new wxh: %d %d %d %d ",x,y, attr.width,attr.height);
#else
    // win32
    GdkWindow* gdkWindow = gtk_widget_get_window(topbar->toplevel);
    HWND parentId = topbar->context->parent_winid;
    HWND child = gdk_win32_window_get_handle(gdkWindow);

    RECT rect = {0};
    GetWindowRect(parentId,&rect);

    int parent_width = rect.right - rect.left;
    int parent_height = rect.bottom - rect.top;

    int child_width = gdk_window_get_width(gdkWindow);
    int child_height = gdk_window_get_height(gdkWindow);

    int nw,nh,cw,ch;
    vcd_topbar_get_no_dpi_size(parent_width,parent_height,&nw,&nh);
    vcd_topbar_get_no_dpi_size(child_width,child_height,&cw,&ch);

    int x = nw/2 - child_width/2;
    int y = 0;
    int w = child_width;
    int h = child_height+1;

    M_DEBUG("parent: [%d x %d] child:[%d %d %d %d]",parent_width,parent_height,x,y,w,h);

    SetParent(child,parentId);
    MoveWindow(child,x,y,w,h,TRUE);
    ShowWindow(child,SW_SHOW);
#endif
}

//GtkWidget*  vcd_topbar_get_buttons(VcdTopbarCtx* context){
//    if(!context) return NULL;

//    VcdTopbar* topbar = g_new0(VcdTopbar,1);
//    vcd_topbar_init(topbar,context);

//    return vcd_topbar_buttons_new(topbar);
//}

static GtkWidget* vcd_topbar_init_buttons(VcdTopbar* topbar){
    GtkOrientation orientation = topbar->context->position==POSITION_TOP ? GTK_ORIENTATION_VERTICAL:GTK_ORIENTATION_HORIZONTAL;

    GtkWidget* box         = vcd_topbar_buttons_new(topbar);
    GtkWidget* vbox        = gtk_box_new(orientation,0);
    GtkWidget* image       = vcd_topbar_new_gtkimage(vcd_revealer_get_img_full_path("topbar-arrow-down-black.png"),24,12,topbar->context->position);
    GtkWidget* event_box   = gtk_event_box_new();
    GtkWidget* revealer    = gtk_revealer_new();
    TopbarPostion position = topbar->context->position;

    //gtk_widget_set_events(event_box,GDK_ALL_EVENTS_MASK);
    gtk_widget_set_events(event_box,GDK_BUTTON_PRESS_MASK|GDK_BUTTON_RELEASE_MASK);


    if(position == POSITION_RIGHT){
        gtk_box_pack_start(GTK_BOX(vbox),event_box,FALSE,FALSE,0);
        gtk_box_pack_start(GTK_BOX(vbox),revealer,FALSE,FALSE,0);
    }else{
        gtk_box_pack_start(GTK_BOX(vbox),revealer,FALSE,FALSE,0);
        gtk_box_pack_start(GTK_BOX(vbox),event_box,FALSE,FALSE,0);
    }
    gtk_container_add(GTK_CONTAINER(event_box),image);
    gtk_container_add(GTK_CONTAINER(revealer), box);


    if(position == POSITION_LEFT){
        gtk_widget_set_halign(GTK_WIDGET(vbox), GTK_ALIGN_START);
        gtk_widget_set_valign(GTK_WIDGET(vbox), GTK_ALIGN_CENTER);
        gtk_revealer_set_transition_type(GTK_REVEALER(revealer),GTK_REVEALER_TRANSITION_TYPE_SLIDE_RIGHT);
        _set_css(box,"box{background-color:rgba(245,245,245,1.0);padding: 10px 0px 10px 0px;border-radius: 0px; border:none}");
    }else if(position == POSITION_RIGHT){
        gtk_widget_set_halign(GTK_WIDGET(vbox), GTK_ALIGN_END);
        gtk_widget_set_valign(GTK_WIDGET(vbox), GTK_ALIGN_CENTER);
        gtk_revealer_set_transition_type(GTK_REVEALER(revealer),GTK_REVEALER_TRANSITION_TYPE_SLIDE_RIGHT);
        _set_css(box,"box{background-color:rgba(245,245,245,1.0);padding: 10px 0px 10px 0px; border-radius: 0px;border:none}");
    }else{
        gtk_widget_set_halign(GTK_WIDGET(vbox), GTK_ALIGN_CENTER);
        gtk_widget_set_valign(GTK_WIDGET(vbox), GTK_ALIGN_START);
        _set_css(box,"box{background-color:rgba(245,245,245,1.0);padding: 0px 10px 0px 10px;border-radius: 0px;border:none}");
    }

    gtk_revealer_set_reveal_child(GTK_REVEALER(revealer),FALSE);

    topbar->revealer = revealer;
    topbar->image = image;
    g_signal_connect(event_box,"button-press-event",G_CALLBACK(_button_pressed),topbar);
    g_signal_connect(event_box,"button-release-event",G_CALLBACK(_button_release),topbar);
    g_signal_connect(event_box,"enter-notify-event",G_CALLBACK(_image_enter_notify),image);
    g_signal_connect(event_box,"leave-notify-event",G_CALLBACK(_image_leave_notify),image);

    _set_css(image,"image{background-color:rgb(240,240,240);border-radius:0px;border:none;}");

    return vbox;
}

GtkWidget*  vcd_topbar_get_buttons(VcdTopbarCtx* context){
    if(!context) return NULL;

    VcdTopbar* topbar = g_new0(VcdTopbar,1);
    vcd_topbar_init(topbar,context);

    GtkWidget* box = vcd_topbar_init_buttons(topbar);

    if(!context->is_show_topbar){
        gtk_widget_set_child_visible(box,FALSE);
    }

    return box;
}

GtkWidget* vcd_topbar_new(VcdTopbarCtx* context){

    if(!context) return NULL;

    // init VcdTopbar
    VcdTopbar* topbar = g_new0(VcdTopbar,1);
    vcd_topbar_init(topbar,context);

    // init toplevel
    // GTK_WINDOW_TOPLEVEL 在linux上正常,在windows上不显示
    GtkWidget* toplevel = gtk_window_new(GTK_WINDOW_POPUP);//GTK_WINDOW_POPUP GTK_WINDOW_TOPLEVEL
    gtk_window_set_keep_above(GTK_WINDOW(toplevel), TRUE);
    gtk_window_set_decorated(GTK_WINDOW(toplevel), FALSE); //无边框
    gtk_window_set_resizable(GTK_WINDOW(toplevel), FALSE);
    gtk_widget_set_size_request(toplevel,200,80);

    topbar->toplevel = toplevel;

    // init buttons
    GtkWidget* topbar_box = vcd_topbar_init_buttons(topbar);
    gtk_container_add(GTK_CONTAINER(toplevel),topbar_box);
    #ifdef __WIN32__
    _set_css(toplevel,"window{opacity:0.0;background-color:rgba(255,0,0,0.0)}");
    #endif

    // must show before vcd_topbar_set_parent
    gtk_widget_show_all(toplevel);

#ifndef __linux__
    Sleep(100);
#endif

    // reparent
    vcd_topbar_set_parent(topbar);

    if(!context->is_show_topbar)
        gtk_widget_set_visible(toplevel,FALSE);

    return topbar->toplevel;
}

